let ImportService = require('./ImportService')
let Mongodb = require('../../database/mongodb');
let counters = require('../../routes/common/counter')

class TestCaseService extends ImportService{
  initParam () {
    this.tableConfigs = [
      { tableName: 'users' }
    ]
    this.enumTypes = ['WORK_TYPE', 'Execution']
  }

  formateData (dataList) {
    return new Promise((resolve, reject) => {
      let rowDatas = []
      let data = {}
      this.getId(dataList.length).then(id => {
        for (let i = 0, len = dataList.length; i < len; i++) {
          data = dataList[i]
          rowDatas.push({
            "id": String(id + i),
            "type": this.dataFormater('WORK_TYPE', data[1], 'value', 'name'),
            "name": data[2],
            "summary": data[3],
            "exUser": this.dataArrFormatter('users', data[4], 'email', 'userName'),
            "state": this.dataFormater('Execution', data[5], 'value', 'name'),
            "planEDate": data[6],
            "orgId": this.user.orgId[0],
            "createTime": new Date(),
            "createUser": this.user.email
          })
        }
        resolve(rowDatas)
      })
    })
  }

  getId (len) {
    return new Promise((resolve, reject) => {
      counters.getLenSequenceValue('work_key', len, (id) => {
        resolve(id)
      })
    })
  }
}

module.exports = new TestCaseService()